<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="6">
          <a-form-item label="用户" v-bind="validateInfos.userId">
	          <j-search-select v-model:value="formData.userId" dict="sys_user,realname,id" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="用户手机号" v-bind="validateInfos.userPhone">
            <a-input v-model:value="formData.userPhone" placeholder="请输入用户手机号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="行驶证" v-bind="validateInfos.drivingLicense">
	          <j-image-upload :fileMax=1 v-model:value="formData.drivingLicense" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="车辆识别代号" v-bind="validateInfos.drivingFVin">
            <a-input v-model:value="formData.drivingFVin" placeholder="请输入车辆识别代号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="住址" v-bind="validateInfos.drivingFAddr">
            <a-input v-model:value="formData.drivingFAddr" placeholder="请输入住址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="发证日期" v-bind="validateInfos.drivingFPubDate">
            <a-input v-model:value="formData.drivingFPubDate" placeholder="请输入发证日期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="发证单位" v-bind="validateInfos.drivingFPubOrg">
            <a-input v-model:value="formData.drivingFPubOrg" placeholder="请输入发证单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="品牌型号" v-bind="validateInfos.drivingFBrandModel">
            <a-input v-model:value="formData.drivingFBrandModel" placeholder="请输入品牌型号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="所有人" v-bind="validateInfos.drivingFAny">
            <a-input v-model:value="formData.drivingFAny" placeholder="请输入所有人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="使用性质" v-bind="validateInfos.drivingFUseProperty">
            <a-input v-model:value="formData.drivingFUseProperty" placeholder="请输入使用性质" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="发动机号码" v-bind="validateInfos.drivingFEngineNo">
            <a-input v-model:value="formData.drivingFEngineNo" placeholder="请输入发动机号码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="号牌号码" v-bind="validateInfos.drivingFVehicleNo">
            <a-input v-model:value="formData.drivingFVehicleNo" placeholder="请输入号牌号码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="注册日期" v-bind="validateInfos.drivingFRegDate">
            <a-input v-model:value="formData.drivingFRegDate" placeholder="请输入注册日期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="检验记录" v-bind="validateInfos.drivingBCheckRecord">
            <a-input v-model:value="formData.drivingBCheckRecord" placeholder="请输入检验记录" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="核定载质量" v-bind="validateInfos.drivingBVgwr">
            <a-input v-model:value="formData.drivingBVgwr" placeholder="请输入核定载质量" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="整备质量" v-bind="validateInfos.drivingBCurbWeight">
            <a-input v-model:value="formData.drivingBCurbWeight" placeholder="请输入整备质量" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="外廓尺寸" v-bind="validateInfos.drivingBLwh">
            <a-input v-model:value="formData.drivingBLwh" placeholder="请输入外廓尺寸" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label=" 核定载人数" v-bind="validateInfos.drivingBPax">
            <a-input v-model:value="formData.drivingBPax" placeholder="请输入 核定载人数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="总质量" v-bind="validateInfos.drivingBGvs">
            <a-input v-model:value="formData.drivingBGvs" placeholder="请输入总质量" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="燃油类型" v-bind="validateInfos.drivingBFuelType">
            <a-input v-model:value="formData.drivingBFuelType" placeholder="请输入燃油类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="准牵引总质量" v-bind="validateInfos.drivingBGcwr">
            <a-input v-model:value="formData.drivingBGcwr" placeholder="请输入准牵引总质量" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="备注" v-bind="validateInfos.drivingBRemark">
            <a-input v-model:value="formData.drivingBRemark" placeholder="请输入备注" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="档案编号" v-bind="validateInfos.drivingBDocNo">
            <a-input v-model:value="formData.drivingBDocNo" placeholder="请输入档案编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="号牌号码" v-bind="validateInfos.drivingBVehicleNo">
            <a-input v-model:value="formData.drivingBVehicleNo" placeholder="请输入号牌号码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="证芯编号" v-bind="validateInfos.drivingBCcn">
            <a-input v-model:value="formData.drivingBCcn" placeholder="请输入证芯编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="道路运输证" v-bind="validateInfos.roadTransportCertificate">
	          <j-image-upload :fileMax=1 v-model:value="formData.roadTransportCertificate" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="道路运输证号" v-bind="validateInfos.roadNo">
            <a-input v-model:value="formData.roadNo" placeholder="请输入道路运输证号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="车牌号码" v-bind="validateInfos.roadVehicleNo">
            <a-input v-model:value="formData.roadVehicleNo" placeholder="请输入车牌号码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="经济类型" v-bind="validateInfos.roadEconType">
            <a-input v-model:value="formData.roadEconType" placeholder="请输入经济类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="经营范围" v-bind="validateInfos.roadBusiRange">
            <a-input v-model:value="formData.roadBusiRange" placeholder="请输入经营范围" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="车辆类型" v-bind="validateInfos.roadVehicleType">
            <a-input v-model:value="formData.roadVehicleType" placeholder="请输入车辆类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="吨座位" v-bind="validateInfos.roadTon">
            <a-input v-model:value="formData.roadTon" placeholder="请输入吨座位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="备注" v-bind="validateInfos.roadRemark">
            <a-input v-model:value="formData.roadRemark" placeholder="请输入备注" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="经营许可证号" v-bind="validateInfos.roadBusiPermitNo">
            <a-input v-model:value="formData.roadBusiPermitNo" placeholder="请输入经营许可证号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="车辆毫米_高" v-bind="validateInfos.roadVehicleMmHeight">
            <a-input v-model:value="formData.roadVehicleMmHeight" placeholder="请输入车辆毫米_高" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="车辆毫米_宽" v-bind="validateInfos.roadVehicleMmWeight">
            <a-input v-model:value="formData.roadVehicleMmWeight" placeholder="请输入车辆毫米_宽" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="车辆毫米_长" v-bind="validateInfos.roadVehicleMmLong">
            <a-input v-model:value="formData.roadVehicleMmLong" placeholder="请输入车辆毫米_长" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="发证日期" v-bind="validateInfos.roadPubDate">
            <a-input v-model:value="formData.roadPubDate" placeholder="请输入发证日期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="地址" v-bind="validateInfos.roadAddr">
            <a-input v-model:value="formData.roadAddr" placeholder="请输入地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="业户名称" v-bind="validateInfos.roadUserName">
            <a-input v-model:value="formData.roadUserName" placeholder="请输入业户名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="初领日期" v-bind="validateInfos.roadFirstDate">
            <a-input v-model:value="formData.roadFirstDate" placeholder="请输入初领日期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="车辆状态" v-bind="validateInfos.vehicleStatus">
	          <j-dict-select-tag v-model:value="formData.vehicleStatus" dictCode="vehicle_status" placeholder="请选择车辆状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="状态" v-bind="validateInfos.status">
	          <j-dict-select-tag v-model:value="formData.status" dictCode="row_status" placeholder="请选择状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../WlhyVehicle.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    userId: '',   
    userPhone: '',   
    drivingLicense: '',   
    drivingFVin: '',   
    drivingFAddr: '',   
    drivingFPubDate: '',   
    drivingFPubOrg: '',   
    drivingFBrandModel: '',   
    drivingFAny: '',   
    drivingFUseProperty: '',   
    drivingFEngineNo: '',   
    drivingFVehicleNo: '',   
    drivingFRegDate: '',   
    drivingBCheckRecord: '',   
    drivingBVgwr: '',   
    drivingBCurbWeight: '',   
    drivingBLwh: '',   
    drivingBPax: '',   
    drivingBGvs: '',   
    drivingBFuelType: '',   
    drivingBGcwr: '',   
    drivingBRemark: '',   
    drivingBDocNo: '',   
    drivingBVehicleNo: '',   
    drivingBCcn: '',   
    roadTransportCertificate: '',   
    roadNo: '',   
    roadVehicleNo: '',   
    roadEconType: '',   
    roadBusiRange: '',   
    roadVehicleType: '',   
    roadTon: '',   
    roadRemark: '',   
    roadBusiPermitNo: '',   
    roadVehicleMmHeight: '',   
    roadVehicleMmWeight: '',   
    roadVehicleMmLong: '',   
    roadPubDate: '',   
    roadAddr: '',   
    roadUserName: '',   
    roadFirstDate: '',   
    vehicleStatus: '',   
    status: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    userId: [{ required: true, message: '请输入用户!'},],
    userPhone: [{ required: true, message: '请输入用户手机号!'},],
    drivingLicense: [{ required: true, message: '请输入行驶证!'},],
    drivingFVin: [{ required: true, message: '请输入车辆识别代号!'},],
    drivingFVehicleNo: [{ required: true, message: '请输入号牌号码!'},],
    roadTransportCertificate: [{ required: true, message: '请输入道路运输证!'},],
    roadNo: [{ required: true, message: '请输入道路运输证号!'},],
    roadVehicleNo: [{ required: true, message: '请输入车牌号码!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
